# ==============================================================================
# file name: analysis-descriptives.R
# authors: Bernhard Clemm
# date: August 24, 2022
# description: descriptives as contained in text; relies on 02-recoding-traces.R and
#              01-recoding-survey.R
# ==============================================================================

# SETUP ========================================================================

basedir <- paste0(dirname(dirname(
  rstudioapi::getSourceEditorContext()$path)), "/")
codedir <- paste0(basedir, "code/")
datadir <- paste0(basedir, "data/")
tabdir <- paste0(basedir, "output/tables/")
figdir <- paste0(basedir, "output/figures/")

library(tidyverse)
library(rstatix)

data_wide <- read.csv(paste0(datadir, "processed/data_wide.csv"))

# Sample size ==================================================================

## PL ####
# n in W2
data_wide %>% filter(country == "PL") %>% nrow()
# n opting in 
data_wide %>% filter(country == "PL") %>% pull(consented) %>% table()
# n completing
data_wide %>% 
  filter(country == "PL" & !is.na(respondent_id_post)) %>% nrow()
data_wide %>% 
  filter(country == "PL" & !is.na(respondent_id_post)) %>% pull(condition) %>% table()

## US ####
# n in W2
data_wide %>% filter(country == "US") %>% nrow()
# n opting in 
data_wide %>% filter(country == "US" & consented == "Consented") %>% pull(condition) %>% table()
# n completing
data_wide %>% 
  filter(country == "US" & !is.na(respondent_id_post)) %>% nrow()
data_wide %>% 
  filter(country == "US" & !is.na(respondent_id_post)) %>% pull(condition) %>% table()

# Expo =========================================================================

## PL ####

data_wide_pl <- data_wide %>% filter(country == "PL")

### Self-reported exposure
# Since we created additive index, compute average by dividing by # of items (9)
mean(data_wide_pl$news_self/9)

### Behavioral exposure
sum(data_wide_pl$u_visits_before, na.rm = T) 
sum(data_wide$news_visits_before, na.rm = T) / 
  sum(data_wide$u_visits_before, na.rm = T)
sum(data_wide_pl$news_visits_before, na.rm = T) / 
  sum(data_wide_pl$u_visits_before, na.rm = T)
                                                 
## US ####

data_wide_us <- data_wide %>% filter(country == "US")

### Self-reported exposure
# Since we created additive index, compute average by dividing by # of items (9)
mean(data_wide_us$news_self/9)

### Behavioral exposure
sum(data_wide_us$u_visits_before, na.rm = T) 
(sum(data_wide_us$news_visits_before, na.rm = T)) / 
  (sum(data_wide_us$u_visits_before, na.rm = T))

# Compliance ===================================================================

compl_trace_summary <- data_wide %>%
  group_by(condition, country) %>%
  get_summary_stats(
    news_before_mean, news_during_mean,
    pol_before_mean, pol_during_mean,
    pol_news_before_mean, pol_news_during_mean) %>%
  mutate(ci_high = mean + 1.96*se,
         ci_low = mean - 1.96*se) %>%
  mutate(variable = gsub("pol_news", "polnews", .$variable)) %>%
  separate(variable, sep = "_", into = c("news", "period", "measure")) 

compl_trace_summary %>% 
  filter(period == "during") %>%
  select(country, condition, news, period, mean) %>%
  arrange(country, news)


